package com.freesky.quartz.task;

import com.freesky.biz.icbc.domain.TLotteryPay;
import com.freesky.biz.icbc.domain.TLotteryPayRefund;
import com.freesky.biz.icbc.mapper.LotteryPayMapper;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.framework.web.service.SendAlarmService;
import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
import com.github.binarywang.wxpay.service.WxPayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 工行520抽奖,退款逻辑
 */
//@Component("lotteryPayTask")
public class IcbcLotteryPayTask {

    private static final Logger logger = LoggerFactory.getLogger(IcbcLotteryPayTask.class);

    @Autowired
    private SendAlarmService sendAlarmService;

    @Autowired
    private WxPayService payService;

    @Autowired
    private LotteryPayMapper lotteryPayMapper;

    public void taskProcess() {
        logger.info("icbcLotteryPayTask task start");
        List<TLotteryPay> lotteryPayList = lotteryPayMapper.selectUnProcessList();
        for(TLotteryPay lotteryPay : lotteryPayList){
            lotteryPayMapper.updateProcessed(lotteryPay.getId());
            refundPay(lotteryPay);
        }
    }

    //微信退款
    public void refundPay(TLotteryPay lotteryPay){
        try{
            lotteryPay.setRefundFlag("1");
            WxPayRefundRequest wxPayRefundRequest = new WxPayRefundRequest();
            wxPayRefundRequest.setTransactionId(lotteryPay.getTransactionId());
            wxPayRefundRequest.setOutTradeNo("ICBCLT"+lotteryPay.getId());
            wxPayRefundRequest.setOutRefundNo(lotteryPay.getId());
            wxPayRefundRequest.setTotalFee(1);
            wxPayRefundRequest.setRefundFee(1);
            wxPayRefundRequest.setNotifyUrl("https://pay.zgrongyidui.com/pay/notify/abChinaBindRefund.do");
            WxPayRefundResult wxPayRefundResult = payService.refund(wxPayRefundRequest);

            TLotteryPayRefund lotteryPayRefund = new TLotteryPayRefund();
            lotteryPayRefund.setId(SeqUtil.getSeqId());
            lotteryPayRefund.setTransactionId(wxPayRefundRequest.getTransactionId());
            lotteryPayRefund.setTradeNo(wxPayRefundRequest.getOutTradeNo());
            lotteryPayRefund.setRefundNo(wxPayRefundRequest.getOutRefundNo());
            lotteryPayRefund.setRefundFee(String.valueOf(wxPayRefundRequest.getRefundFee()));

            lotteryPayRefund.setResultCode(wxPayRefundResult.getResultCode());
            lotteryPayRefund.setResultMsg(wxPayRefundResult.getReturnMsg());

            if(wxPayRefundResult.getResultCode().equalsIgnoreCase("SUCCESS")){
                lotteryPay.setUseFlag("0");
                lotteryPayRefund.setRefundNo(wxPayRefundResult.getRefundId());
                lotteryPay.setProcessed("2");
            } else {
                lotteryPay.setProcessed("3");
            }
            lotteryPayMapper.updatePayResult(lotteryPay);
            lotteryPayMapper.addPayRefund(lotteryPayRefund);

        } catch (Exception ioe){
            ioe.printStackTrace();
            logger.error(ioe.getMessage());
            sendAlarmService.sendAlarmMail("250083682@qq.com","【异常】JOB-工行520抽奖-refundPay",ioe.getMessage());
        }
    }
}
